//
//  CityVC.swift
//  旅航ios
//
//  Created by tulip on 2021/5/19.
//

import UIKit
import SwiftyJSON

class CityVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
  
    var isDepart:Bool!
    var cityArr:Array<JSON>=[]
    var gn_table:UITableView!
    var gj_table:UITableView!
    @IBOutlet weak var searchText: UITextField!
    @IBOutlet weak var line2: UIView!
    @IBOutlet weak var line1: UIView!
    @IBOutlet weak var guojiBtn: UIButton!
    @IBOutlet weak var guoneiBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        gn_table=UITableView.init(frame: CGRect(x: 0, y: line1.frame.origin.y+25, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-(line1.frame.origin.y+25)))
        self.view.addSubview(gn_table)
        gn_table.register(UINib(nibName: "CityListTableCell", bundle: nil), forCellReuseIdentifier: "common")
        gn_table.delegate=self
        gn_table.dataSource=self
        
        gj_table=UITableView.init(frame: CGRect(x: 0, y: line1.frame.size.height+line1.frame.origin.y+25, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-(line1.frame.size.height+line1.frame.origin.y+25)))
        
        Network.request(.getCity) { (JSON) in
            self.cityArr=self.cityArr + JSON["data"].array!
            self.cityArr.append(JSON["data"])
            self.gn_table.reloadData()
        } failure: { (err) in
            
        }

        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CityListTableCell=tableView.dequeueReusableCell(withIdentifier: "common", for: indexPath) as! CityListTableCell
        cell.isDepart=isDepart
        cell.closeView={[weak self]() in
            self?.dismiss(animated: true, completion: nil)

        }
        if cityArr.count > 0{
            cell.setBtn(n: cityArr.count, dic: cityArr)
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       print(CGFloat(ceilf(Float(cityArr.count)/4.0)*44+40))
        return CGFloat(ceilf(Float(cityArr.count)/4.0)*44+40)
    }

    @IBAction func close(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
        
    }
    
}
